<template>
  <div ref="chart"  :style="chartStyle">
  </div>
</template>

<script>
import echarts from 'echarts';

export default {
  name: 'customcar',
  props: {
    config: {
      required: true,
    },
    chartStyle: {
      require: false,
      type: Object,
      default: () => ({
        height: '100%',
        position: 'absolute',
        width: '100%',
        top: '0px',
        bottom: '0px',
        left: '0px',
      }),
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {

  },
  mounted() {
    this.renderChart();
  },
  methods: {
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    renderChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs.chart, this.config.theme);
      }

      const option = this.makeOptions(this.config, this.chartData);
      this.chart.clear();
      this.chart.setOption(option);
      setTimeout(() => {
        this.chart.resize();
      }, 100);
    },
    makeOptions() {
      const data = [];
      for (let i = 0; i < 6; i += 1) {
        data.push(Math.round(Math.random() * 10));
      }
      const data2 = [100, 100, 100, 100, 100, 100];
      const path = 'path://M1390,595h79l-39,22Z';
      const myColor = ['#ff68bd', '#ef73ef', '#c37ef5', '#8787ff', '#1bdef2', '#37edc4'];

      for (let i = 0; i < data.length; i += 1) {
        if (Math.random() <= 100) {
          data[i] = Math.round(Math.random() * 60);
        } else {
          data[i] = Math.round(Math.random() * 80);
        }
      }

      return {
        backgroundColor: '#273454',
        title: [
          {
            text: 'Project',
            x: '20',
            y: '20',
            textStyle: {
              fontSize: '30',
              fontWeight: '100',
              color: '#9c9c9c',
            },
          },
          {
            text: 'Infographic',
            x: '120',
            y: '20',
            textStyle: {
              fontSize: '30',
              fontWeight: '100',
              color: '#b7bbc6',
            },
          },
        ],
        grid: {
          left: 100,
          top: 100,
        },
        xAxis: {
          axisLabel: {
            interval: 0,
            textStyle: {
              color(param, index) {
                return myColor[index];
              },
              fontSize: 16,
              fontFamily: 'FZYaoti',
              fontWeight: 100,
            },
            margin: 5,
          },
          position: 'top',
          splitLine: {
            show: true,
            lineStyle: {
              // type: 'dotted',
              color: '#9c9c9c',
            },
            interval: 0,
          },
          axisLine: {
            lineStyle: {
              color: '#9c9c9c',
              width: 1,
            },
          },
          splitArea: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          data: ['2017', '2018', '2019', '2020', '2021', '2022'],
        },
        yAxis: {
          axisLine: {
            lineStyle: {
              color: '#9c9c9c',
              width: 1,
            },
          },
          axisLabel: {
            interval: 0,
            formatter: '{value}%',
            textStyle: {
              color: '#b7bbc6',
              fontFamily: 'FZYaoti',
            },
            margin: 20,
          },
          splitLine: {
            show: false,
          },
          axisTick: {
            lineStyle: {
              color: '#b7bbc6',
              width: 1,
              height: 5,
            },
          },
        },
        series: [
          {
            type: 'bar',
            barWidth: 60,
            barGap: '-100%',
            stack: '广告',
            itemStyle: {
              color(params) {
                const colorList = [
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#ff68bd',
                  }, {
                    offset: 0.5,
                    color: '#ff68bd',
                  }, {
                    offset: 0.5,
                    color: '#9e3b72',
                  }, {
                    offset: 1,
                    color: '#9e3b72',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#ef73ef',
                  }, {
                    offset: 0.5,
                    color: '#ef73ef',
                  }, {
                    offset: 0.5,
                    color: '#883e89',
                  }, {
                    offset: 1,
                    color: '#883e89',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#c37ef5',
                  }, {
                    offset: 0.5,
                    color: '#c37ef5',
                  }, {
                    offset: 0.5,
                    color: '#724892',
                  }, {
                    offset: 1,
                    color: '#724892',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#8787ff',
                  }, {
                    offset: 0.5,
                    color: '#8787ff',
                  }, {
                    offset: 0.5,
                    color: '#5959b1',
                  }, {
                    offset: 1,
                    color: '#5959b1',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#1bdef2',
                  }, {
                    offset: 0.5,
                    color: '#1bdef2',
                  }, {
                    offset: 0.5,
                    color: '#0a8491',
                  }, {
                    offset: 1,
                    color: '#0a8491',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#37edc4',
                  }, {
                    offset: 0.5,
                    color: '#37edc4',
                  }, {
                    offset: 0.5,
                    color: '#1b8970',
                  }, {
                    offset: 1,
                    color: '#1b8970',
                  }]),
                ];
                return colorList[params.dataIndex];
              },
            },
            data,
          },
          {
            name: '',
            type: 'pictorialBar',
            symbol: path,
            symbolSize: [60, 20],
            symbolOffset: [0, 0],
            z: 12,
            symbolPosition: 'end',
            itemStyle: {
              color: '#3c4765',
              opacity: 1,
            },
            data: data2,
          },
          {
            name: '',
            type: 'pictorialBar',
            symbol: path,
            symbolSize: [60, 20],
            symbolOffset: [0, 20],
            z: 12,
            itemStyle: {
              opacity: 1,
              color(params) {
                const colorList = [
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#ff68bd',
                  }, {
                    offset: 0.5,
                    color: '#ff68bd',
                  }, {
                    offset: 0.5,
                    color: '#9e3b72',
                  }, {
                    offset: 1,
                    color: '#9e3b72',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#ef73ef',
                  }, {
                    offset: 0.5,
                    color: '#ef73ef',
                  }, {
                    offset: 0.5,
                    color: '#883e89',
                  }, {
                    offset: 1,
                    color: '#883e89',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#c37ef5',
                  }, {
                    offset: 0.5,
                    color: '#c37ef5',
                  }, {
                    offset: 0.5,
                    color: '#724892',
                  }, {
                    offset: 1,
                    color: '#724892',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#8787ff',
                  }, {
                    offset: 0.5,
                    color: '#8787ff',
                  }, {
                    offset: 0.5,
                    color: '#5959b1',
                  }, {
                    offset: 1,
                    color: '#5959b1',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#1bdef2',
                  }, {
                    offset: 0.5,
                    color: '#1bdef2',
                  }, {
                    offset: 0.5,
                    color: '#0a8491',
                  }, {
                    offset: 1,
                    color: '#0a8491',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#37edc4',
                  }, {
                    offset: 0.5,
                    color: '#37edc4',
                  }, {
                    offset: 0.5,
                    color: '#1b8970',
                  }, {
                    offset: 1,
                    color: '#1b8970',
                  }]),
                ];
                return colorList[params.dataIndex];
              },
            },
            data: [1, 1, 1, 1, 1, 1],
          },
          {
            name: '',
            type: 'pictorialBar',
            symbol: path,
            symbolSize: [60, 20],
            symbolOffset: [0, 0],
            z: 12,
            label: {
              show: true,
              formatter: '{c}%',
              position: 'top',
              distance: 5,
              color: '#fff',
              fontFamily: 'FZYaoti',
              fontWeight: 100,
              // textShadowColor: 'rgba(255, 255, 255, .6)',
              fontSize: 16,
              // textShadowBlur: '0',
              // textShadowOffsetX: 1,
              // textShadowOffsetY: 1,
            },
            itemStyle: {
              opacity: 1,
              color(params) {
                const colorList = [
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#d2589d',
                  }, {
                    offset: 0.5,
                    color: '#d2589d',
                  }, {
                    offset: 0.5,
                    color: '#d04d98',
                  }, {
                    offset: 1,
                    color: '#d04d98',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#c160c1',
                  }, {
                    offset: 0.5,
                    color: '#c160c1',
                  }, {
                    offset: 0.5,
                    color: '#bc52be',
                  }, {
                    offset: 1,
                    color: '#bc52be',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#a16ac8',
                  }, {
                    offset: 0.5,
                    color: '#a16ac8',
                  }, {
                    offset: 0.5,
                    color: '#995fc5',
                  }, {
                    offset: 1,
                    color: '#995fc5',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#7c7ae5',
                  }, {
                    offset: 0.5,
                    color: '#7c7ae5',
                  }, {
                    offset: 0.5,
                    color: '#7171e3',
                  }, {
                    offset: 1,
                    color: '#7171e3',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#1bb7c6',
                  }, {
                    offset: 0.5,
                    color: '#1bb7c6',
                  }, {
                    offset: 0.5,
                    color: '#0ab1c1',
                  }, {
                    offset: 1,
                    color: '#0ab1c1',
                  }]),
                  new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#2ebe9d',
                  }, {
                    offset: 0.5,
                    color: '#2ebe9d',
                  }, {
                    offset: 0.5,
                    color: '#21bb99',
                  }, {
                    offset: 1,
                    color: '#21bb99',
                  }]),
                ];
                return colorList[params.dataIndex];
              },
            },
            symbolPosition: 'end',
            data,
          },
          {
            name: '',
            type: 'effectScatter',
            rippleEffect: {
              period: 1,
              scale: 5,
              brushType: 'fill',
            },
            z: 20,
            symbolPosition: 'end',
            symbol: path,
            symbolSize: [15, 5],
            symbolOffset: [0, 9],
            itemStyle: {
              normal: {
                shadowColor: 'rgba(0, 0, 0, .3)',
                shadowBlur: 5,
                shadowOffsetY: 3,
                shadowOffsetX: 0,
                color: '#fff',
              },
            },
            data,
          },
          {
            name: '2019',
            type: 'bar',
            barWidth: 60,
            barGap: '-100%',
            z: 0,
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                  offset: 0,
                  color: '#2e3b5b',
                }, {
                  offset: 0.5,
                  color: '#2e3b5b',
                }, {
                  offset: 0.5,
                  color: '#3d4864',
                }, {
                  offset: 1,
                  color: '#3d4864',
                }],
                global: false,
              },
              opacity: 1,
            },
            data: data2,
          },
        ],
      };
    },
  },
  watch: {
    config: {
      deep: true,
      handler() {
        if (this.config.needResize) {
          this.config.needResize = false;
          this.handleResize();
        }
      },
    },
  },
};
</script>

<style scoped lang="stylus">

</style>
